package Yang.homework3;

public class SellTicketRunnable implements Runnable {
    private static Demo de = new Demo();
    private static int ticket = 100;
    @Override
    public void run() {
        while (true) {
            synchronized (de) {
                if (ticket > 0) {
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "卖出第" + ticket + "张票！");
                    ticket--;
                } else {
                    System.out.println(Thread.currentThread().getName() + "余票不足！");
                    break;
                }
            }
        }
    }
}
class Demo{}